package com.sinohb.autonomousdriving_test;

import android.util.Log;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class JsonFileProcessor {
    // 定义 Line 类
    public static class Line {
        public int id;
        public int style;
        public int color;
        public float c0 = -1000;
        public float c1;
        public float c2;
        public float c3;
    }

    // 定义 JsonWrapper 用于 Line 类型
    public static class LineJsonWrapper {
        public List<Line> value0;
    }

    // 定义 Model 类
    public static class Model {
        public int id;
        public int type;
        public float vDist;
        public float hDist;
        public float yawAngle;
    }

    // 定义 JsonWrapper 用于 Model 类型
    public static class ModelJsonWrapper {
        public List<Model> value0;
    }

    /**
     * 处理指定目录下的所有JSON文件，检测类型并输出结果
     */
    public static List<FileWithNumber> processJsonFiles(String directoryPath) {
        File folder = new File(directoryPath);
        if (!folder.exists() || !folder.isDirectory()) {
            Log.e("JsonFileProcessor", "文件夹不存在或不是一个目录: " + directoryPath);
            return null;
        }

        File[] files = folder.listFiles();
        if (files == null) {
            Log.e("JsonFileProcessor", "无法读取文件夹内容: " + directoryPath);
            return null;
        }

        // 创建文件列表并按数字排序
        List<FileWithNumber> fileList = new ArrayList<>();
        for (File file : files) {
            if (file.isFile() && file.getName().endsWith(".json")) {
                String fileName = file.getName();
                int dotIndex = fileName.indexOf('.');
                if (dotIndex > 0) {
                    String numberStr = fileName.substring(0, dotIndex);
                    try {
                        int number = Integer.parseInt(numberStr);
                        fileList.add(new FileWithNumber(number, file));
                    } catch (NumberFormatException e) {
                        Log.w("JsonFileProcessor", "跳过无法解析数字的文件: " + fileName);
                    }
                } else {
                    Log.w("JsonFileProcessor", "跳过没有数字前缀的文件: " + fileName);
                }
            }
        }

        // 按数字排序
        Collections.sort(fileList, new Comparator<FileWithNumber>() {
            @Override
            public int compare(FileWithNumber o1, FileWithNumber o2) {
                return Integer.compare(o1.number, o2.number);
            }
        });
        return fileList;
    }

    // 辅助类，用于存储数字和文件
    static class FileWithNumber {
        int number;
        File file;

        FileWithNumber(int number, File file) {
            this.number = number;
            this.file = file;
        }
    }

    /**
     * 读取文件内容为字符串
     */
    public static String readFileAsString(String filePath) {
        File file = new File(filePath);
        if (!file.exists()) {
            Log.e("JsonFileProcessor", "文件不存在: " + filePath);
            return null;
        }

        StringBuilder stringBuilder = new StringBuilder();
        try (FileInputStream fis = new FileInputStream(file);
             InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8);
             BufferedReader br = new BufferedReader(isr)) {

            String line;
            while ((line = br.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (Exception e) {
            Log.e("JsonFileProcessor", "读取文件错误: " + e.getMessage());
            return null;
        }

        return stringBuilder.toString();
    }
}